package k8s

import (
	"github.com/gin-gonic/gin"
	node_req "kubeimooc/model/node/request"
	"kubeimooc/response"
)

type NodeApi struct {
}

/*
*
更新node的污点
*/
func (na *NodeApi) UpdateNodeTaint(c *gin.Context) {
	var updatedTaint node_req.UpdatedTaint
	if err := c.ShouldBindJSON(&updatedTaint); err != nil {
		response.FailWithMessage(c, err.Error())
		return
	}
	err := nodeService.UpdateNodeTaint(updatedTaint)
	if err != nil {
		response.FailWithMessage(c, err.Error())
	} else {
		response.Success(c)
	}
}

/*
*
更新node的标签
*/
func (na *NodeApi) UpdateNodeLabel(c *gin.Context) {
	var updatedLabel node_req.UpdatedLabel
	if err := c.ShouldBindJSON(&updatedLabel); err != nil {
		response.FailWithMessage(c, err.Error())
		return
	}
	err := nodeService.UpdateNodeLabel(updatedLabel)
	if err != nil {
		response.FailWithMessage(c, err.Error())
	} else {
		response.Success(c)
	}
}

/*
*
获取nodeDetail 或者 nodeList
*/
func (na *NodeApi) GetNodeDetailOrList(c *gin.Context) {
	var nodeName = c.Query("nodeName")
	var keyword = c.Query("keyword")
	if nodeName != "" {
		//获取node详情
		detail, err := nodeService.GetNodeDetail(nodeName)
		if err != nil {
			response.FailWithMessage(c, err.Error())
			return
		} else {
			response.SuccessWithDetail(c, "获取node详情成功", detail)
			return
		}
	} else {
		//获取列表
		list, err := nodeService.GetNodeList(keyword)
		if err != nil {
			response.FailWithMessage(c, err.Error())
			return
		} else {
			response.SuccessWithDetail(c, "获取node列表成功", list)
			return
		}
	}
}
